package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.work.keep.R;
import com.work.keep.adapter.MyTopicActivityAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.TopicActivityData;
import com.work.keep.ui.PullToRefreshRecyclerView;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * GET https://api.gotokeep.com/v1.1/hashtag/?type=hot&perPage=10 HTTP/1.1
 * X-X: 82241e05792e6420a945b049783997f95de556f4
 * X-KEEP-FROM: android
 * X-KEEP-TIMEZONE: America/New_York
 * X-KEEP-CHANNEL: keep
 * X-KEEP-VERSION: 2.9.1
 * Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzQ0NDgyNywiZXhwIjoxNDU2MDM2ODI3LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.qcYVm3A8SPdiMkjVpp_ZwjLKw0Skws4m2yozvOctsQM
 * User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1; Google Nexus 5 - 5.1.0 - API 22 - 1080x1920 Build/LMY47D) Paros/3.2.13
 * Host: api.gotokeep.com
 * Connection: Keep-Alive
 * Created by dllo on 16/1/22.
 */
public class TopicActivity extends BaseActivity {
    PullToRefreshRecyclerView messageActivityRecycler;
    private RecyclerView recyclerView;
    private ImageView topicActivityLeft;
    private String topicUrl = "https://api.gotokeep.com/v1.1/hashtag/?type=hot&perPage=10";
    private VolleySingleton volleySingleton;
    private MyTopicActivityAdapter myTopicActivityAdapter;
    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView() {
        messageActivityRecycler = (PullToRefreshRecyclerView) findViewById(R.id.topicActivityRecycler);
        recyclerView = (RecyclerView) messageActivityRecycler.getRefreshableView();
        topicActivityLeft = (ImageView) findViewById(R.id.topicActivityLeft);
    }

    @Override
    protected void initData() {
        volleySingleton = VolleySingleton.getInstance();
        topicActivityLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        myTopicActivityAdapter = new MyTopicActivityAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myTopicActivityAdapter);
        GsonRequest<TopicActivityData> topicRequest = new GsonRequest<TopicActivityData>(topicUrl, TopicActivityData.class, new Response.Listener<TopicActivityData>() {
            @Override
            public void onResponse(TopicActivityData response) {
                myTopicActivityAdapter.addData(response.getData());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzQ0NDgyNywiZXhwIjoxNDU2MDM2ODI3LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.qcYVm3A8SPdiMkjVpp_ZwjLKw0Skws4m2yozvOctsQM");
                map.put("Host", "api.gotokeep.com");
                map.put("X-X", "82241e05792e6420a945b049783997f95de556f4");
                return map;
            }
        };
        volleySingleton._addRequest(topicRequest);
    }
}
